﻿using System;
using TS.DataAccess;
using TS.Entities;
using System.Data.SqlClient;
using System.Collections.ObjectModel;


namespace TS.Bussiness
{
    public class UserBussiness
    {
        /// <summary>
        /// Get Role Of User in Database
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public User GetUserLogOn(string account, string password)
        {
            if (string.IsNullOrEmpty(account)) return null;
            if (string.IsNullOrEmpty(password)) return null;
            UserDataAccess userDao = new UserDataAccess();
            try
            {
                User user = userDao.GetUser(account, password);
                return user;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }



        /// <summary>
        /// get User by UserId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public User GetUserByUserId(int userId)
        {
            var userDao = new UserDataAccess();
            try
            {
                User user = userDao.GetUserByUserId(userId);
                return user;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        public User GetRole(int userId, string page)
        {
            var userDao = new UserDataAccess();
            try
            {
                User user = userDao.GetRole(userId, page);
                return user;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        /// <summary>
        /// Insert User
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int InsertUser(User user)
        {
            if (string.IsNullOrEmpty(user.UserName)) return -1;
            if (string.IsNullOrEmpty(user.Password)) return -1;
            UserDataAccess userDao = new UserDataAccess();
            try
            {
                return userDao.InsertUser(user);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public int UpdateUser(User user)
        {
            if (string.IsNullOrEmpty(user.UserName)) return -1;
            UserDataAccess userDao = new UserDataAccess();
            try
            {
                return userDao.UpdateUser(user);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public int UpdateIsLock(User user)
        {
            UserDataAccess userDao = new UserDataAccess();
            try
            {
                return userDao.UpdateIsLock(user);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        /// <summary>
        /// Select User Pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<User> ReadUserPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy,string isLock, out int totalRecord)
        {
            totalRecord = 0;
            var dataAccess = new UserDataAccess();
            try
            {
                if (pageIndex >= 0 && pageSize > 0)
                {
                    var result = dataAccess.ReadUserPaged(pageSize, pageIndex, strFilter, strOrderBy,isLock, out totalRecord);
                    if (result != null)
                    {
                        return result;
                    }
                    return null;
                }
                return null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }
    }
}
